# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
from __future__ import unicode_literals

import ctypes
import winnotify.winapi as winapi

from winnotify.winapi_constants import CW_USEDEFAULT
from winnotify.winapi_constants import NIF_GUID
from winnotify.winapi_constants import NIF_INFO
from winnotify.winapi_constants import NIF_SHOWTIP
from winnotify.winapi_constants import NIF_TIP
from winnotify.winapi_constants import NIIF_INFO
from winnotify.winapi_constants import NIIF_RESPECT_QUIET_TIME
from winnotify.winapi_constants import NIM_ADD
from winnotify.winapi_constants import NIM_DELETE
from winnotify.winapi_constants import NIM_SETVERSION
from winnotify.winapi_constants import NOTIFYICON_VERSION_4

hwnd = winapi.CreateWindowEx(
    dwExStyle=0,
    lpClassName="Static",
    lpWindowName="Test Window",
    dwStyle=0,
    X=CW_USEDEFAULT,
    Y=CW_USEDEFAULT,
    nWidth=CW_USEDEFAULT,
    nHeight=CW_USEDEFAULT,
)

if hwnd == 0:
    e = ctypes.WinError()
    raise e

notifyIconData = winapi.NOTIFYICONDATA()
notifyIconData.cbSize = ctypes.sizeof(notifyIconData)
notifyIconData.hWnd = hwnd
notifyIconData.uID = 0
notifyIconData.uFlags = NIF_TIP | NIF_INFO | NIF_GUID | NIF_SHOWTIP
notifyIconData.uCallbackMessage = 0
notifyIconData.hIcon = 0
notifyIconData.szTip = "Denver's Icon!"
notifyIconData.dwState = 0
notifyIconData.dwStateMask = 0
notifyIconData.uVersion = NOTIFYICON_VERSION_4
notifyIconData.szInfo = u"C'est mon premi\u00e8re balloon notification"
notifyIconData.szInfoTitle = u"My First Notification Icon!"
notifyIconData.dwInfoFlags = NIIF_INFO | NIIF_RESPECT_QUIET_TIME
byteArray8 = ctypes.c_byte * 8
guidItemData4 = byteArray8(0xbe, 0x4e, 0x37, 0x38, 0xed, 0xb2, 0xe7, 0x2c)
notifyIconData.guidItem = winapi.GUID(0x162d5960, 0x5e63, 0x4b57, guidItemData4)
notifyIconData.hBalloonIcon = None

success = winapi.Shell_NotifyIcon(
    dwMessage=NIM_ADD,
    lpdata=ctypes.pointer(notifyIconData),
)
if not success:
    raise Exception("Shell_NotifyIcon(NIM_ADD) failed")

success = winapi.Shell_NotifyIcon(
    dwMessage=NIM_SETVERSION,
    lpdata=ctypes.pointer(notifyIconData),
)
if not success:
    raise Exception("Shell_NotifyIcon(NIM_SETVERSION) failed")

raw_input("Press ENTER to remove the icon and exit")

success = winapi.Shell_NotifyIcon(
    dwMessage=NIM_DELETE,
    lpdata=ctypes.pointer(notifyIconData),
)
if not success:
    raise Exception("Shell_NotifyIcon(NIM_DELETE) failed")
